import { Pipe, PipeTransform, NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { Utils } from '../utils/utils';

/**
 * 本管道用于格式化日期，缺省格式为yyyy-MM-dd
@example
 * {{job.createTime | date:'yyyy-MM-dd hh:mm'}} //按指定的pattern(yyyy-MM-dd hh:mm)格式化日期
 * {{job.createTime | date}} //按缺省的pattern(yyyy-MM-dd)格式化日期
 */
@Pipe( { name: 'date' } )
export class DatePipe implements PipeTransform {
    transform( date: Date, pattern?: string ): string {
        if ( date ) {
            if ( !pattern ) {
                pattern = "yyyy-MM-dd";
            }
            return Utils.formatDate( date, pattern );
        } else {
            return "";
        }
    }
}

@NgModule( {
    imports: [CommonModule],
    exports: [DatePipe],
    declarations: [DatePipe]
} )
export class DatePipeModule { }